package VIEW;

import CONF.ConexaoOracle;
import MODEL.*;
import java.util.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class LineUpView extends javax.swing.JFrame {

    /**
     * Creates new form LineUpView
     */
    public LineUpView() throws SQLException {
        initComponents();

        populatesTree();
    }

    private void populatesTree() throws SQLException {

        RockInRio rir = new RockInRio();
        ArrayList<LineUpModel> lineUps;
        ArrayList<ShowModel> shows;
        ConexaoOracle conOracle;
        Connection con;

        conOracle = new ConexaoOracle();
        con = ConexaoOracle.conectar();

        lineUpArray = rir.getLineUpsDB(con);


        DefaultMutableTreeNode lineUpTreeNode = new DefaultMutableTreeNode("");
        DefaultTreeModel treeModel = new DefaultTreeModel(lineUpTreeNode);
        lineUpTree.setModel(treeModel);

        for (LineUpModel lineUp : lineUpArray) {
            DefaultMutableTreeNode dataTreeNode = new DefaultMutableTreeNode(lineUp.calendarToDataString(lineUp.getData()));
            lineUpTreeNode.add(dataTreeNode);
            
            
            for(ShowModel show : lineUp.getShowsDB(con)){
                DefaultMutableTreeNode localTreeNode = new DefaultMutableTreeNode(show.getLocal());
                dataTreeNode.add(localTreeNode);
                
                for(BandaModel banda : show.getBandasDB(con)){
                    DefaultMutableTreeNode bandaTreeNode = new DefaultMutableTreeNode(banda.getNome());
                    localTreeNode.add(bandaTreeNode);
                }
            }
        }

        lineUpTree.setRootVisible(false);
        expandAll(lineUpTree, true);
    }

    static public void expandAll(JTree tree, boolean expand) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        expandAll(tree, new TreePath(root), expand);
    }

    static private void expandAll(JTree tree, TreePath parent, boolean expand) {

        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {

            for (@SuppressWarnings("unchecked") Enumeration<TreeNode> e = node.children(); e.hasMoreElements();) {
                TreeNode treeNode = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(treeNode);
                expandAll(tree, path, expand);
            }
        }

        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        lineUpTree = new javax.swing.JTree();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Rock In Rio - LineUps");

        jButton1.setText("Voltar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButton1)
        );

        jScrollPane3.setViewportView(lineUpTree);

        jButton2.setText("Prosseguir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(83, 83, 83)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        EscolhaView Escolha = new EscolhaView();
        Escolha.setLocationRelativeTo(null); 
        Escolha.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
        DisponibilidadeView Disponibilidade = new DisponibilidadeView(lineUpArray);
        Disponibilidade.setLocationRelativeTo(null);
        Disponibilidade.setVisible(true);
        /*
         * String[] teste = new String[6];
         *
         * teste[0] = "cu"; teste[1] = "anus"; teste[2] = "caralho"; teste[3] =
         * "buceta"; teste[4] = "vagina"; teste[5] = "cacete";          *
         * DisponibilidadeView Disponibilidade = new DisponibilidadeView();
         *
         * Disponibilidade.setVisible(true);
         *
         * DisponibilidadeView.setDefaultLookAndFeelDecorated(true);
         * Disponibilidade.setLayout(new FlowLayout());
         * Disponibilidade.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         * JCheckBox [] ac = new JCheckBox[teste.length];
         *
         * for(int i = 0 ; i < teste.length ; i++ ){
         *
         * ac[0] = new JCheckBox(teste[i]); ac[0].setVisible(true);
         * //ac[i].setSelected(true); JCheckBox ac1; ac1 = ac[0];
         *
         * Disponibilidade.add(ac1); Disponibilidade.pack();
         * Disponibilidade.setVisible(true);
         *
         *
         * }
         */
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LineUpView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LineUpView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LineUpView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LineUpView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new LineUpView().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(LineUpView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTree lineUpTree;
    public ArrayList<LineUpModel> lineUpArray;
    // End of variables declaration//GEN-END:variables
}
